<template>
  <div class="pages brandbrandClass" id="app">
    <div class="pageLevel">
      <!-- 操作框 -->
      <div class="operationBox tableSearch">
        <el-button type="primary" v-if="setRole('新增')" :disabled="dis" @click="add()" size="mini">新增</el-button>
        <el-button type="primary" v-if="setRole('删除')" :disabled="dis" @click="deleteRows()" size="mini" >删除</el-button>
      </div>
      <!-- 搜索框 -->
      <div class="tableSearch">
        <el-form label-width="100px" class="demo-ruleForm">
          <el-form-item label="" label-width="15px">
            <el-input class="input" :disabled="dis" clearable v-model="selectdata.keyword" size="small" placeholder="输入品牌名称或编号"></el-input>
          </el-form-item>
          <el-form-item label="" label-width="15px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="searchSelect" :disabled="dis" class="Button">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 表格 -->
      <div class="tableMain">
        <el-table ref="multipleTable" :data="tableData" @selection-change="handleSelectionChange" stripe style="width: 100%">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column type="index" label="序号" width="55"></el-table-column>
          <el-table-column prop="code" label="品牌编号" align="center">
            <template slot-scope="scope">
              <!-- <el-input v-show="scope.row.show==true" class="input" v-model="scope.row.code" placeholder="请输入品牌编号" clearable size="small"></el-input> -->
              <span>{{scope.row.code}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="typeId" align="center" label="品牌名称">
            <template slot-scope="scope">
              <el-input clearable maxlength="50" v-show="scope.row.show==true" v-model="scope.row.name" placeholder="请输入品牌名称" size="small"></el-input>
              <!--<el-select v-show="scope.row.show==true" v-model="scope.row.typeId" placeholder="请选择" size="small">
							<el-option v-for="item in supplierType" :key="item.id" :label="item.name" :value="item.id"></el-option>
						</el-select>-->
              <span v-show="scope.row.show==false">{{scope.row.name}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="name" align="center" label="操作" width="180" >
            <template slot-scope="scope">
              <el-button size="small" v-if="setRole('修改')" :disabled="dis" v-show="scope.row.show==false" @click="handleEdit(scope.$index,scope.row.id)" type="text" class="addDetail" >修改</el-button>
              <el-button size="small" v-show="scope.row.show==true" @click="save(scope.$index)" type="text" class="addDetail">保存</el-button>
              <el-button size="small" v-show="scope.row.show==true" @click="cancel(scope.$index)" type="text" class="addDetail">取消</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 页码 -->
        <div class="pageblock">
          <el-button :disabled="dis" size="small" type="primary" class="topage" @click="toPage">确定</el-button>
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[20,30]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalItems">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
import {
  getGoodsLabelList,
  saveGoodsLabel,
  updateGoodsLabel,
  deleteGoodsLabel
} from "@/api";
import util from "@/util";
export default {
  computed: mapState(["merchantId", "sub","power"]),
  data() {
    return {
      dis: false,
      selectdata: {
        keyword: ""
      },
      selectdatas: {
        keyword: ""
      },
      searchData: "",
      supplierType: [],
      tableData: [],
      multipleSelection: [],
      // 分页开始
      total: 0,
      pageSize: 20,
      totalItems: 0,
      currentPage: 1,
      ids: ""
    };
  },
  created() {
    // 获取品牌分类名称列表
    this.order();
  },
  mounted() {
    // 获取表格数据
    this.tableget(20, 1);
  },
  methods: {
     //配置权限
    setRole (key) {
            return util.selectTitle(this.power, key)
        },
    // 获取品牌分类名称列表
    order() {
      this.supplierType = [
        { id: 1, name: "总代理" },
        { id: 2, name: "省代理" }
      ];
    },
    // 获取表格数据
    tableget(pageSize, pageNum) {
      let data = {
        name: this.selectdatas.keyword,
        pageSize: pageSize,
        pageNo: pageNum,
        labelType: "0",
        shopId: this.merchantId
      };
      getGoodsLabelList(data).then(res => {
        if(res.code=="200"){
          this.totalItems = res.data.total;
        for (let i = 0; i < res.data.list.length; i++) {
          res.data.list[i].show = false;
        }
        this.tableData = res.data.list;
        }
      });
    },
    // 新增-添加表的一行
    add() {
      this.dis = true;
      let newele = {
        code: "",
        typeId: "",
        describe: "",
        show: true
      };
      this.tableData.unshift(newele);
    },
    // 保存-向后台提交新加的一条数据-？？是否要验证空字段
    save(i) {
      if (this.tableData[i].name == "" || this.tableData[i].name == undefined) {
        this.$message({
          type: "error",
          message: "请输入品牌名称"
        });
      } else {
        let postdata = {
          code: this.tableData[i].code,
          name: this.tableData[i].name,
          labelType: "0",
          id: this.ids,
          shopId: this.merchantId
        };
        if (this.ids != "") {
          updateGoodsLabel(postdata).then(res => {
            if(res.code=="200"){
              this.dis = false;
            this.tableData[i].show = false;
            this.ids = "";
            this.tableget(20, 1);
            this.$message({
              type: "success",
              message: res.msg
            });
            }
          });
        } else {
          saveGoodsLabel(postdata).then(res => {
           if(res.code=="200"){
              this.dis = false;
            this.tableData[i].show = false;
            this.ids = "";
            this.tableget(20, 1);
            this.$message({
              type: "success",
              message: res.msg
            });
           }
          });
        }
      }
    },
    // 取消-将表格新加的一条数据删除
    cancel(i) {
      this.dis = false;
      this.tableget(20, 1);
    },
    // 多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 删除
    deleteRows() {
      if (this.multipleSelection.length > 0) {
        this.$confirm("确定要删除吗?", "提示", {
          center: true,
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          closeOnClickModal: false,
          closeOnPressEscape: false
        }).then(() => {
          var arr = [];
          for (var i = 0; i < this.multipleSelection.length; i++) {
            arr.push(this.multipleSelection[i].id);
          }
          let data = {
            id: arr.toString()
          };
          deleteGoodsLabel(data).then(res => {
           if(res.code=="200"){
              this.tableget(20, 1);
             this.$message({
              type: "success",
              message: res.msg
             });
           }
          });
        });
      } else {
        this.$message({
          type: "error",
          message: "请选择品牌"
        });
      }
    },
    // 查询
    searchSelect() {
      this.selectdatas = this.selectdata;
      this.tableget(20, 1);
    },
    //修改
    handleEdit(i, id) {
      this.ids = id;
      this.dis = true;
      this.tableData[i].show = true;
    },
    //跳页
    toPage() {
      this.tableget(this.pageSize, this.currentPage);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.tableget(this.pageSize, 1);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.tableget(this.pageSize, this.currentPage);
    }
  }
};
</script>
<style scoped>
.showmore {
  display: block;
}
#app{
    text-align: left;
}
</style>

